package com.lbssoft.gpsbd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbssoft.gpsbd.dao.DeviceMapper;
import com.lbssoft.gpsbd.datalink.Context;
import com.lbssoft.gpsbd.model.Device;
import com.lbssoft.gpsbd.model.GroupDeviceTree;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.DeviceVo;
import com.lbssoft.gpsbd.model.vo.Move;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.DeviceService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nczc on 2018/3/15.
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    public DeviceMapper deviceMapper;

    @Override
    public Result add(Device device) {
        Result result = new Result();
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        if(device.getName().isEmpty()){
            device.setName(device.getImei().substring(device.getImei().length()-6,device.getImei().length()));
        }
        deviceMapper.addDevice(device);
        deviceMapper.addUserDevice(user.getId(), device.getId());
        result.setCode(0);
        result.setData(device.getId());
        //初始化数据
        Context.dataManager.init();
        return result;
    }

    @Override
    public Result delete(long id) {
        Result result = new Result();
        Device dev = deviceMapper.getDeviceById(id);
        if(dev==null){
            result.setCode(1);
            result.setMsg("DeviceDoesNotExist");
            return result;
        }
        deviceMapper.delDevice(id);
        result.setCode(0);
        return result;
    }

    @Override
    public Result update(Device device) {
        Result result = new Result();
        Device dev = deviceMapper.getDeviceById(device.getId());
        if(dev==null){
            result.setCode(1);
            result.setMsg("DeviceDoesNotExist");
            return result;
        }

        deviceMapper.updateDevice(device);
        result.setCode(0);
        return result;
    }

    @Override
    public Result list() {
        Result result = new Result();
        UserVo user = (UserVo)SecurityUtils.getSubject().getPrincipal();
        List<Device> devices = deviceMapper.getDevicesByUid(user.getId());
        result.setCode(0);
        result.setData(devices);
        return result;
    }

    @Override
    public List<GroupDeviceTree> getTree(long userId) {
        return deviceMapper.getTree(userId);
    }


    @Override
    public Device getInfo(long id) {
        return deviceMapper.getDeviceById(id);
    }

    @Override
    public void move(Move move) {
        deviceMapper.move(move);
    }

    @Override
    public Result getDevices(int page,int pageSize,String column,String keyword) {
        Result result = new Result();
        String where = "";

        if(!StringUtils.isNullOrEmpty(column)&&!StringUtils.isNullOrEmpty(keyword)){
            where = " where "+column+" LIKE '%"+keyword+"%'";
        }
        PageHelper.startPage(page, pageSize);
        List<DeviceVo> list = deviceMapper.getDevices(where);
        PageInfo pageInfo = new PageInfo(list);
        Page pages = (Page) list;
        Map data = new HashMap();
        data.put("page", page);
        data.put("total", pageInfo.getTotal());
        data.put("list", pages);
        result.setData(data);
        return result;
    }
}
